'use strict';

'use strict';

const app = require('koa')();
const path = require('path');

const cfgUtil = require('./../server/commons/factories/configuration');
const logUtil = require('./../server/commons/factories/logFactory');
const serverCfg = cfgUtil.get('server');
const localsCfg = cfgUtil.get('locals');
const appLog = logUtil.getLogger('app');

const NODE_ENV = serverCfg.NODE_ENV;

//koa全局异常处理
const onerror = require('koa-onerror');
app.on('error', function(err, ctx) {
    appLog.error(serverCfg.APP_NAME + ' server error', err, ctx);
});
onerror(app);
appLog.info('回调函数中增加onerror');

//jade模板配置
const views = require('koa-views');
app.use(views({
    root: serverCfg.VIEWS_DIR,
    default: 'jade'
}));
appLog.info('jade模板配置完成');

//session中间件
//const redisStore = require('koa-redis')(serverCfg.redis);
//app.redis = redisStore.client;
//appLog.debug('redis配置完成');
const session = require('koa-generic-session');
//app.use(session(extend({
//  store: redisStore,
//}, serverCfg.session)));
app.use(session(serverCfg.session));
appLog.info('session中间件加载完成');

//对提交的数据格式进行解析
const bodyparser = require('koa-bodyparser');
app.use(bodyparser());
appLog.info('bodyParser中间件加载完成');

//输出json数据格式
const json = require('koa-json');
app.use(json());
appLog.info('json中间件加载完成');

if (NODE_ENV === 'development') {
    //日志
    // const koaBunyanLogger = require('koa-bunyan-logger');
    // app.use(logger());
    // app.use(koaBunyanLogger());
    // app.use(koaBunyanLogger.requestIdContext());
    // app.use(koaBunyanLogger.requestLogger());
    appLog.info('logger中间件加载完成');

    //配置stylus
    const middleware = require('koa-stylus');
    const stylus = require('stylus');
    const nib = require('nib');
    app.use(middleware({
        src: serverCfg.CLIENT_DIR,
        dest: serverCfg.STATIC_DIR,
        compile: function(str, path) {
            return stylus(str)
                .set('filename', path)
                .define('url', stylus.url({
                    paths: [serverCfg.STATIC_DIR]
                }))
                .define('$STATIC_PATH', localsCfg.STATIC_PATH)
                .use(nib());
        }
    }));
    appLog.info('stylus中间件加载完成');

    //livereload
    if (!serverCfg.livereload.disabled) {
        const livereload = require('koa-livereload');
        const options = Object.assign({}, serverCfg.livereload);
        delete options.disabled;
        app.use(livereload(options));
        appLog.info('livereload中间件加载完成');
    }
}

//静态目录配置
const serve = require('koa-static');
app.use(serve(serverCfg.STATIC_DIR));
appLog.info('static中间件加载完成');

//网页静态图标
const favicon = require('koa-favicon');
app.use(favicon(path.join(serverCfg.STATIC_DIR, 'favicon.ico')));
appLog.info('favicon中间件加载完成');

//设置全局模板渲染数据
const locals = Object.assign({}, cfgUtil.get('locals'), {
    NODE_ENV: NODE_ENV
});
app.use(function*(next) {
    Object.assign(this.state, locals);
    yield next;
});

//配置路由
const routes = require('./routes');
app.use(routes.routes());
appLog.info('路由加载完成');

app.name = serverCfg.APP_NAME;
app.env = serverCfg.NODE_ENV;

module.exports = app;